<?php

namespace App\Http\Controllers\User;

use App\Http\Classes\UserRemember;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RememberController extends Controller
{
    /*
     * 添加多个单词到用户已学会词库
     * POST
     * words(array)
     */
    public function addRememberWords(Request $request)
    {
        try
        {
            if ( $request->has('words') )
            {
                $words = $request->get('words');
                if ( is_array($words) )
                {
                    $data = [];
                    for ( $i = 0; $i < count($words); $i++ )
                    {
                        $data[$i]['word_id'] = $words[$i];
                        $data[$i]['type'] = 1;
                        $data[$i]['user_id'] = $_COOKIE['USER_ID'];
                    }
                    $remember = new UserRemember();

                    if ( $remember->addRememberWords($data) )
                    {
                        return $this->response(1, '添加成功');
                    }
                    else
                    {
                        return $this->response(0, '添加失败', [], $remember->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '参数不规范');
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 添加多条句子到用户已学会句库
     * POST
     * sentences(array)
     */
    public function addRememberSentences(Request $request)
    {
        try
        {
            if ( $request->has('sentences') )
            {
                $sentences = $request->get('sentences');
                if ( is_array($sentences) )
                {
                    $data = [];
                    for ( $i = 0; $i < count($sentences); $i++ )
                    {
                        $data[$i]['sentence_id'] = $sentences[$i];
                        $data[$i]['type'] = 2;
                        $data[$i]['user_id'] = $_COOKIE['USER_ID'];
                    }
                    $remember = new UserRemember();
                    if ( $remember->addRememberSentences($data) )
                    {
                        return $this->response(1, '添加成功');
                    }
                    else
                    {
                        return $this->response(0, '添加失败', [], $remember->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '参数不规范');
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户所有已学会单词
     * GET
     */
    public function getUserAllRememberWords()
    {
        try
        {
            $remember = new UserRemember($_COOKIE['USER_ID']);

            $remembers = $remember->getUserAllRemmberWords();
            if ($remembers)
            {
                return $this->response(1, '获取成功', $remembers);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $remember->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户所有已学会句子
     * GET
     */
    public function getUserAllRememberSentences()
    {
        $remember = new UserRemember($_COOKIE['USER_ID']);

        $remembers = $remember->getUserAllRemmberSentences();
        if ($remembers)
        {
            return $this->response(1, '获取成功', $remembers);
        }
        else
        {
            return $this->response(0, '无匹配数据', [], $remember->getMessage());
        }
    }

    /*
     * 分页方式获取用户熟词库
     * GET
     * orderBy  string
     * order    string
     * index    int
     * limit    int
     *
     */
    public function getUserRememberWordsWithPaging(Request $request)
    {
        try
        {
            $data = [];
            $data['orderBy'] = $request->has('orderBy') ? $request->get('orderBy') : 'id';
            $data['order'] = $request->has('order') ? $request->get('order') : 'asc';
            $data['index'] = $request->has('index') ? $request->get('index') : 1;
            $data['limit'] = $request->has('limit') ? $request->get('limit') : 20;
            $data['order'] = ($data['order'] != 'asc') && ($data['order'] != 'desc') ? 'asc' : $data['order'];

            $remember = new UserRemember($_COOKIE['USER_ID']);
            $remembers = $remember->getUserRememberWordsWithPaging($data);

            if ( $remembers )
            {
                return $this->response(1, '获取成功', $remembers);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $remember->getMessage());
            }

        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 分页方式获取用户熟句库
     * GET
     * orderBy  string
     * order    string
     * index    int
     * limit    int
     *
     */
    public function getUserRememberSentencesWithPaging(Request $request)
    {
        try
        {
            $data = [];
            $data['orderBy'] = $request->has('orderBy') ? $request->get('orderBy') : 'id';
            $data['order'] = $request->has('order') ? $request->get('order') : 'asc';
            $data['index'] = $request->has('index') ? $request->get('index') : 1;
            $data['limit'] = $request->has('limit') ? $request->get('limit') : 20;
            $data['order'] = ($data['order'] != 'asc') && ($data['order'] != 'desc') ? 'asc' : $data['order'];

            $remember = new UserRemember($_COOKIE['USER_ID']);
            $remembers = $remember->getUserRememberWordsWithPaging($data);

            if ( $remembers )
            {
                return $this->response(1, '获取成功', $remembers);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $remember->getMessage());
            }

        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
